package dp.备忘录dfs;

import java.util.LinkedList;

/**
 * todo
 *
 * @author songZiHao
 * @version 1.0.0
 * @ClassName 等差数列划分413.java
 * @createTime 2024年04月02日 17:20:00
 */
public class 等差数列划分413 {
	public static int numberOfArithmeticSlices(int[] nums) {
		if (nums.length < 3) {
			return 0;
		}
		int count = 0;
		for (int i = 0; i < nums.length - 2; i++) {
			if (nums[i + 2] - nums[i + 1] == nums[i + 1] - nums[i]) {
				int diff = nums[i + 1] - nums[i];
				list.add(nums[i]);
				list.add(nums[i + 1]);
				list.add(nums[i + 2]);
				backTrace(nums, i + 3, nums.length, diff);
				count += (list.size() - 2);
			}
			list.clear();
		}
		return count;
	}

	static LinkedList<Integer> list = new LinkedList<>();

	public static void backTrace(int[] nums, int curIndex, int length, int diff) {
		//到了末尾停止
		if (curIndex >= length) {
			return;
		}
		if ((nums[curIndex] - nums[curIndex - 1]) == diff) {
			list.add(nums[curIndex]);
			backTrace(nums, curIndex + 1, length, diff);
		} else {
			return;
		}
	}

	public static void main(String[] args) {
		numberOfArithmeticSlices(new int[] {1, 2, 3, 4});
	}
}
